package com.liuheng.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import com.liuheng.manager.service.CaptchaService;
import com.liuheng.spzx.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CaptchaServiceImpl implements CaptchaService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ValidateCodeVo generateCaptcha() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150,48,4,20);
        String code = captcha.getCode();
        String imageBase64 = captcha.getImageBase64();
        String key= UUID.randomUUID().toString().replace("-","");
        redisTemplate.opsForValue().set("user:login:captcha"+key,code,1, TimeUnit.MINUTES);
        ValidateCodeVo vo = new ValidateCodeVo();
        vo.setCodeKey(key);
        vo.setCodeValue("data:image/png;base64," + imageBase64);
        return vo;
    }
}
